﻿import QtQuick 2.0
import QtQuick.Window 2.12

import "../components"
import "../sampleManager"
import "../dataAnalysis"
import "../runningDisplay"


Item {



    readonly property real dp: config.dp

    id: root
    anchors.fill: parent

    property int stepPage: 0        // 0:实验信息 1:样本板 2:运行 3:完成
    property int stepModel: 0       // 0:设备空 1:新建 2:运行中 3:已完成

    Connections{
        target: mainwindow
        onTabExperiment:{
            refresh()
        }
    }
    Connections{
        target: pageExperiment
        onReanalysis: {
            cDataAnalysis.analysisConfig()
        }onStartExperimentSuccess: {
            cRunningDisplay.initEquPage(equId)
        }
    }

    // 调用：1. 设备切换  2. 刷新当前显示页面
    function refresh(){

        if(gCountAll === 0)     return
        if(gPageModel === 1){              // 新建模式
            stepPage = gNewStep
            stepModel = 1
        }
        if(gPageModel === 0){               // 闲逛模式
            if(gCurDeviceId===""){
                return
            }
            stepPage = gDeviceStep
            if(gDeviceInfoList[gCurDeviceId]["EquStatus"]===0){
                stepModel = 0
            }else if(gDeviceInfoList[gCurDeviceId]["EquStatus"]===1){     // 空闲
                stepModel = 0
            }else if(gDeviceInfoList[gCurDeviceId]["EquStatus"]===2){     // 运行中
                stepModel = 2
            }else if(gDeviceInfoList[gCurDeviceId]["EquStatus"]===3){     // 已完成
                stepModel = 3
            }
        }

        // 实验信息刷新
        if(stepPage === 0 && (gPageModel===1 || gCurDeviceStatus>1)){
            cExperimentInfo.refresh()
        }
        // 样本板刷新
        else if(stepPage === 1 && (gPageModel===1 || gCurDeviceStatus>1)){
            if(gPageModel===1){
                if(gIsNewOpenRefresh_sample){  // 样本板根据当前的gSampleInfo刷新
                    cSampleManager.setSampleList(gSampleList)
                    gIsNewOpenRefresh_sample = false
                }else if(gIsNewRefresh_sample){                  // 重新刷新样本板
                    cSampleManager.initSampleList()
                    gIsNewRefresh_sample = false
                }
            }else if(gPageModel === 0){
                cSampleManager.setSampleList(CExpInfo.getSampleInfo(gCurDeviceId))
            }
        }
        // 运行实验刷新
        else if(stepPage === 2 && gCurDeviceStatus>1){
            cRunningDisplay.setEquId(gCurDeviceId)
        }
        // 数据分析刷新
        else if(stepPage === 3 && gCurDeviceStatus>1){
            cDataAnalysis.tabAnalysis(gCurDeviceId)
            if(root.visible){
                CAnalysis.openAnalysis(gCurDeviceId)
            }
        }
    }

    Component.onCompleted: {
        var list = layout_tab.children
        for(let i = 0; i < list.length; i ++){
            list[i].button_color = "transparent"
            list[i].text_color = "#666666"
            list[i].button_color_select = "white"
            list[i].text_color_select = "#1677FF"
        }
    }
    Rectangle{          // tab
        height: 60*dp
        width: parent.width
        color: "#EFEFF2"
        Row{
            id: layout_tab
            anchors.fill: parent
            PushButton{
                height: 60*dp
                width: 120*dp
                title: "实验信息"
                isSelected: stepPage === 0
                onReleased: {
                    changeStepPage(0)
                }
            }PushButton{
                height: 60*dp
                width: 120*dp
                title: "样本板"
                isSelected: stepPage === 1
                onReleased: {
                    changeStepPage(1)
                }
            }PushButton{
                height: 60*dp
                width: 120*dp
                title: "运行"
                visible: stepModel === 2
                isSelected: stepPage === 2
                onReleased: {
                    changeStepPage(2)
                }
            }PushButton{
                height: 60*dp
                width: 120*dp
                title: "结果"
                visible: stepModel === 3
                isSelected: stepPage === 3
                onReleased: {
                    changeStepPage(3)
                }
            }
        }
    }

    Rectangle{              // 主页
        id: layout_page
        y: 60*dp
        height: parent.height-120*dp
        width: parent.width

        // 实验信息
        ExperimentInfo{
            id: cExperimentInfo
            visible: stepPage === 0
        }
        // 样本板
        SampleManager{
            id: cSampleManager
            isEditable: gPageModel === 1
            visible: stepPage === 1
            onSampleChanged: {
                gSampleList = cSampleManager.getSampleList()
            }
        }
        // 运行
        RunningDisplay{
            id: cRunningDisplay
            visible: stepPage === 2
        }
        // 数据分析
        DataAnalysis{
            id: cDataAnalysis
            visible: stepPage === 3
        }
        z: 1

    }

    Rectangle{              // 底边栏
        id: layout_button
        y: parent.height-60*dp + 1
        height: 60*dp
        width: parent.width

        Item{           // Page0 实验信息 =================================
            anchors.fill: parent
            PushButton{
                y: 10*dp
                x: parent.width-140*dp
                title: "下一步"
                defaultMode2: true
                onReleased: {
                    changeStepPage(1)
                }
            }
            visible: stepPage === 0
        }
        Item{           // Page1 样本板 =================================
            anchors.fill: parent
            PushButton{
                y: 10*dp
                x: 20*dp
                title: "上一步"
                defaultMode2: true
                onReleased: {
                    changeStepPage(0)
                }
            }
//            PushButton{
//                y: 10*dp
//                x: parent.width-420*dp
//                title: "打开实验"
//                visible: gPageModel === 1
//                defaultMode2: true
//                onReleased: {
//                    let res = CWlk.openWlkFile_getInfo()
//                    if(res["Res"] === undefined || res["Res"] === 2){
//                        return
//                    }else if(res["Res"].toString() === "0"){
//                        mainwindow.showToast("文件格式错误", 1)
//                        return
//                    }else{
//                        let sampleList = res["SampleInfo"]
//                        gSampleList = sampleList
//                        cSampleManager.setSampleList(sampleList)
//                        mainwindow.showToast("打开成功", 0)
//                    }
//                }
//            }
            PushButton{
                y: 10*dp
                x: parent.width-280*dp
                title: "保存"
                visible: gPageModel === 1
                defaultMode2: true
                onReleased: {
                    onButtonSave()
                }
            }PushButton{
                y: 10*dp
                x: parent.width-140*dp
                title: "启动"
                visible: gPageModel === 1
                defaultMode1: true
                onReleased: {
                    pageExperiment.startExperiment()
                }
            }
            visible: stepPage === 1
        }
        Item{           // Page2 运行 =================================
            anchors.fill: parent
            PushButton{
                y: 10*dp
                x: 20*dp
                title: "上一步"
                defaultMode2: true
                onReleased: {
                    changeStepPage(1)
                }
            }PushButton{
                y: 10*dp
                x: parent.width-280*dp
                title: "查看扩增程序"
                defaultMode2: true
                onReleased: {
                    onButtonCheckAmplProgram()
                }
            }PushButton{
                y: 10*dp
                x: parent.width-140*dp
                title: "停止实验"
                defaultMode1: true
                onReleased:{
                    pageExperiment.stopExperiment()
                }
            }
            visible: stepPage === 2
        }
        Item{           // Page3 结果 =================================
            anchors.fill: parent
            PushButton{
                y: 10*dp
                x: 20*dp
                title: "上一步"
                defaultMode2: true
                onReleased: {
                    changeStepPage(1)
                }
            }PushButton{
                y: 10*dp
                x: cDataAnalysis.curPage === 3 ? parent.width-280*dp : parent.width-420*dp
                title: "分析配置"
                defaultMode2: true
                onReleased: {
                    onButtonReAnalysis()
                }
            }PushButton{
                y: 10*dp
                x: parent.width-280*dp
                title: "导出图片"
                visible: cDataAnalysis.curPage !== 3
                defaultMode2: true
                onReleased: {
                    onButtonExportImage()
                }
            }PushButton{
                y: 10*dp
                x: parent.width-140*dp
                title: "导出报告"
                defaultMode1: true
                onReleased: {
                    onButtonExportReport()
                }
            }
            visible: stepPage === 3
        }

        CustomBorder{
            tBorderwidth: 1
            borderColor: "#E6E6E6"
        }
    }

}
